﻿using Lemon.Blog.Account.Dtos;
using Lemon.Blog.Blog;
using Lemon.Blog.Users;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Identity;
using SkiaSharp;
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;

namespace Lemon.Blog.Account
{
    public class MyAccountService: BlogAppService, IMyAccountService
    {
        private readonly IdentityUserManager _identityUserManager;
        private readonly IRepository<AppUser> _appUsers;
        private readonly IWebHostEnvironment _webHostEnvironment;

        public MyAccountService(IdentityUserManager identityUserManager,
            IRepository<AppUser> appUsers,
            IWebHostEnvironment webHostEnvironment)
        {
            this._identityUserManager = identityUserManager;
            this._appUsers = appUsers;
            this._webHostEnvironment = webHostEnvironment;
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task UpdateUserInfo(UpdateUserInfoDto dto)
        {
            try
            {
                var identityUser = await _identityUserManager.GetByIdAsync(dto.Id);
                identityUser.Name = dto.Name;
                await _identityUserManager.UpdateAsync(identityUser);
                var user = await _appUsers.FirstOrDefaultAsync(t => t.Id == dto.Id);
                user.Sex = dto.Sex;
                user.DateOfBirth = dto.DateOfBirth;
                user.Signature = dto.Signature;
                await _appUsers.UpdateAsync(user);
            }
            catch (Exception ex)
            {
                throw new UserFriendlyException(ex.Message);
            }
        }

        /// <summary>
        /// 获取当前登录用户的信息
        /// </summary>
        /// <returns></returns>
        public async Task<UserInfoDto> GetUserInfo()
        {
            if (!CurrentUser.IsAuthenticated)
                throw new UserFriendlyException("未登录");
            AppUser appUser = await _appUsers.FindAsync(t => t.Id == CurrentUser.Id);
            UserInfoDto result = ObjectMapper.Map<AppUser, UserInfoDto>(appUser);
            result.Roles = string.Join(',', CurrentUser.Roles);
            return result;
        }

        /// <summary>
        /// 通过username查询用户信息
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public async Task<UserInfoDto> GetUserInfoByUsername(string username)
        {
            AppUser appUser = await _appUsers.FindAsync(t => t.UserName == username);
            UserInfoDto result = ObjectMapper.Map<AppUser, UserInfoDto>(appUser);
            result.Email = "";//隐藏邮箱信息
            return result;
        }

        [HttpGet]
        public async Task<string> UpdateHeadPhoto(string url)
        {
            string fullUrl = url;
            fullUrl = fullUrl.Replace("temp-", "");

            url = "wwwroot" + url[url.IndexOf("/img")..];
            string newName = url.Replace("temp-", "");
            if (File.Exists(url))
            {
                File.Delete(newName);
                File.Move(url, newName);
                AppUser appUser = await _appUsers.FindAsync(t => t.Id == CurrentUser.Id);
                appUser.HeadPhoto = fullUrl;
                await _appUsers.UpdateAsync(appUser);
                return fullUrl;
            }
            else
                throw new UserFriendlyException("图片路径错误");
        }
    }
}
